#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <omp.h>

int main(){
 
  FILE * pFile;
  long lSize;
  char * pch=0;
  char * buffer;
  char * buffer2;
  size_t result;

  
  pFile = fopen ( "shakespe.txt" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
  buffer2 = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate

  fclose (pFile);
  
  int i,j=0;
  
  pch = strtok (buffer," \t");

  while (pch != NULL){
    strcat(buffer2,pch);
    pch = strtok (NULL," \t");
    j++;
  }
     
  free (buffer);

  size_t b2 = strlen(buffer2);
    

  for(i=0; i < b2 ;i++){
    if(ispunct(buffer2[i]))
      buffer2[i]='\n';
  }
  
  pFile = fopen ( "res.txt" , "w" );    
  fwrite (buffer2 , 1 , b2 , pFile );

  free (buffer2);
  fclose (pFile);
  
  return 0;
}